package com.seveneight.baijiesteward.utils;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seveneight.baijiesteward.base.MyApplication;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.bean.VersionBean;
import com.seveneight.baijiesteward.http.HttpConfig;

/**
 * Created by user1 on 2018/2/23.
 */

public class Setting {
    private static Setting settings;
    private Context context;
    private ACache aCache;

    public synchronized static Setting getInstance(Context context) {
        if (settings == null) {
            settings = new Setting(context);
        }
        return settings;
    }

    public Setting(Context context) {
        this.context = context;
        aCache = ACache.get(context);
    }

    public boolean isLogin() {
        return (boolean) SPUtils.getParam(context, "isLogin", false);
    }

    public void setIsLogin(boolean isLogin) {
        SPUtils.setParam(context, "isLogin", isLogin);
        aCache.clear();
    }

    public String getUserID() {
        if (isLogin()) {
            if (!StringUtils.isEmpty(PreferencesUtil.get(context,PreferencesUtil.userId,""))){
                return PreferencesUtil.get(context,PreferencesUtil.userId,"");
            }else {
                return aCache.getAsString("UserID");
            }
//            return aCache.getAsString("UserID");
        } else {
            return "";
        }
    }


    public void setHeadImgUrl(String url) {
//        SPUtils.setParam(context, "HeadImgUrl", url);
        aCache.put("HeadImgUrl", HttpConfig.SOURCE_URL + url);
    }

    public String getHeadImgUrl() {
        return aCache.getAsString("HeadImgUrl");
    }

    public void setUserInfo(UserBean bean) {
        setIsLogin(true);
        aCache.put("UserID", bean.getUserID());
        aCache.put("MerchantNo", bean.getMerchantNo());
        aCache.put("RealName", bean.getRealName());
        aCache.put("IdCardNo", bean.getIdCardNo());
        aCache.put("Email", bean.getEmail());
        aCache.put("Address", bean.getAddress());
        aCache.put("WRate", bean.getWRate());
        aCache.put("WBrokerage", StringFormat.formatNumber(bean.getWBrokerage()));
        aCache.put("AbleBalance", StringFormat.formatNumber(bean.getAbleBalance()));
        aCache.put("UnAbleBalance", StringFormat.formatNumber(bean.getUnAbleBalance()));
        aCache.put("WBalance", StringFormat.formatNumber(bean.getWBalance()));
        aCache.put("WAbleBalance", StringFormat.formatNumber(bean.getWAbleBalance()));
        aCache.put("WUnAbleBalance", StringFormat.formatNumber(bean.getWUnAbleBalance()));
        aCache.put("ParentId1", bean.getParentId1());
        aCache.put("ParentId2", bean.getParentId2());
        aCache.put("ParentId3", bean.getParentId3());
        aCache.put("MobilePhone", bean.getMobilePhone());
        SPUtils.setParam(context, "AuthStatus", bean.getAuthStatus());
        if (!TextUtils.isEmpty(bean.getHeadImgUrl())) {
            aCache.put("HeadImgUrl", HttpConfig.SOURCE_URL + bean.getHeadImgUrl());
        } else {
            aCache.put("HeadImgUrl", bean.getHeadImgUrl());
        }
        if (!TextUtils.isEmpty(bean.getIdCardImgUrl())) {
            aCache.put("IdCardImgUrl", HttpConfig.SOURCE_URL + bean.getIdCardImgUrl());
        } else {
            aCache.put("IdCardImgUrl", bean.getIdCardImgUrl());
        }
        if (!TextUtils.isEmpty(bean.getBankCardImgUrl())) {
            aCache.put("IdCardImgUrl", HttpConfig.SOURCE_URL + bean.getBankCardImgUrl());
        } else {
            aCache.put("IdCardImgUrl", bean.getBankCardImgUrl());
        }
        if (!TextUtils.isEmpty(bean.getHoldIdCardImgUrl())) {
            aCache.put("IdCardImgUrl", HttpConfig.SOURCE_URL + bean.getHoldIdCardImgUrl());
        } else {
            aCache.put("IdCardImgUrl", bean.getHoldIdCardImgUrl());
        }
        aCache.put("AuthTime", DateUtil.formatDate(bean.getAuthTime()));
        aCache.put("RankId", bean.getRankId());
        aCache.put("Rate", bean.getRate());
        aCache.put("Brokerage", StringFormat.formatNumber(bean.getBrokerage()));
        aCache.put("Balance", StringFormat.formatNumber(bean.getBalance()));
        aCache.put("DeleteMark", bean.isDeleteMark());
        aCache.put("EnabledMark", bean.isEnabledMark());
        aCache.put("RegisterTime", DateUtil.formatDate(bean.getRegisterTime()));
        LogUtil.d("写入用户信息完成");
    }

    public UserBean getUserInfo() {
        UserBean bean = new UserBean();
        bean.setEmail(aCache.getAsString("Email"));
        bean.setAddress(aCache.getAsString("Address"));
        bean.setWRate(aCache.getAsString("WRate"));
        bean.setWBrokerage(aCache.getAsString("WBrokerage"));
        bean.setAbleBalance(aCache.getAsString("AbleBalance"));
        bean.setUnAbleBalance(aCache.getAsString("UnAbleBalance"));
        bean.setWBalance(aCache.getAsString("WBalance"));
        bean.setWAbleBalance(aCache.getAsString("WAbleBalance"));
        bean.setWUnAbleBalance(aCache.getAsString("WUnAbleBalance"));
        bean.setMerchantNo(aCache.getAsString("MerchantNo"));
        bean.setUserID(aCache.getAsString("UserID"));
        bean.setRealName(aCache.getAsString("RealName"));
        bean.setIdCardNo(aCache.getAsString("IdCardNo"));
        bean.setParentId1(aCache.getAsString("ParentId1"));
        bean.setParentId2(aCache.getAsString("ParentId2"));
        bean.setParentId3(aCache.getAsString("ParentId3"));
        bean.setMobilePhone(aCache.getAsString("MobilePhone"));
        bean.setHeadImgUrl(aCache.getAsString("HeadImgUrl"));
        bean.setRegisterTime(aCache.getAsString("RegisterTime"));
        bean.setAuthStatus((String) SPUtils.getParam(context, "AuthStatus", "0"));
        bean.setIdCardImgUrl(aCache.getAsString("IdCardImgUrl"));
        bean.setBankCardImgUrl(aCache.getAsString("BankCardImgUrl"));
        bean.setHoldIdCardImgUrl(aCache.getAsString("HoldIdCardImgUrl"));
        bean.setAuthTime(aCache.getAsString("AuthTime"));
        bean.setRankId(aCache.getAsString("RankId"));
        bean.setRate(aCache.getAsString("Rate"));
        bean.setBrokerage(aCache.getAsString("Brokerage"));
        bean.setBalance(aCache.getAsString("Balance"));
        bean.setDeleteMark(Boolean.parseBoolean(aCache.getAsString("DeleteMark")));
        bean.setEnabledMark(Boolean.parseBoolean(aCache.getAsString("EnabledMark")));
        return bean;
    }

    public void setVersion(VersionBean version) {
        SPUtils.setParam(context, "versioncode", version.getVersionCode());
        SPUtils.setParam(context, "versionname", version.getVersionName());
        SPUtils.setParam(context, "updateinfo", version.getUpdateInfo());
        SPUtils.setParam(context, "updateurl", version.getUpdateUrl());
    }

    public VersionBean getVersion() {
        VersionBean versionBean = new VersionBean();
        versionBean.setVersionCode((int) SPUtils.getParam(context, "versioncode", 0));
        versionBean.setVersionName((String) SPUtils.getParam(context, "versionname", ""));
        versionBean.setUpdateInfo((String) SPUtils.getParam(context, "updateinfo", ""));
        versionBean.setUpdateUrl((String) SPUtils.getParam(context, "updateurl", ""));
        LogUtil.d(versionBean.toString());
        return versionBean;
    }
}
